﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Nirvana
{
    public class WaitLoadSceneAsync : WaitLoadAssetProgress
    {
        private AssetBundleManager abMgr;
        private AsyncOperation asyncOperation;
        private string sceneName;
        private string fileName;
        private LoadSceneMode sceneMode;
        public AssetBundleManager AssetBundleMgr
        {
            get { return abMgr; }
        }

        public AsyncOperation AsyncOperation
        {
            get { return asyncOperation; }
        }

        public WaitLoadSceneAsync(AssetBundleManager mgr, string sceneName, string fileName, LoadSceneMode sceneMode)
        {
            abMgr = mgr;
            this.sceneName = sceneName;
            this.fileName = fileName;
            this.sceneMode = sceneMode;
        }
        public WaitLoadSceneAsync(string format, params object[] args)
        {
            base.Error = string.Format(format, args);
        }
        public override float Progress
        {
            get
            {
                return (this.asyncOperation == null) ? 0f : this.asyncOperation.progress;
            }
        }

        public override bool keepWaiting
        {
            get
            {
                if (this.asyncOperation == null && base.Error != null)
                {
                    return false;
                }
                bool flag = this.asyncOperation != null && this.asyncOperation.isDone;
                return !flag;
            }
        }


        public override bool Load()
        {

            AssetBundleItem u = this.abMgr.LoadAssetBundleItem(this.sceneName);
            if (u == null)
            {
                return true;
            }
            if (!string.IsNullOrEmpty(u.error))
            {
                base.Error = u.error;
                return false;
            }
            this.asyncOperation = SceneManager.LoadSceneAsync(this.sceneName, this.sceneMode);
            return false;
        }
    }
}

